﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using qlHieuThuocDongY.BusinessLogic;
using qlHieuThuocDongY.BusinessObject;

namespace qlHieuThuocDongY
{
    public partial class frmNhapThuoc : Form
    {

        private ThuocNhapBUS thuocnhapBUS = new ThuocNhapBUS();
        private NhaCungCapBUS nhacungcapBUS = new NhaCungCapBUS();
        private DanhMucThuocBUS dmThuocBUS = new DanhMucThuocBUS();
        public frmNhapThuoc()
        {
            InitializeComponent();
        }

        private void frmNhapThuoc_Load(object sender, EventArgs e)
        {
          
            // Load combox mã thuốc
            cboMaThuoc.DataSource = dmThuocBUS.LayDanhMucThuoc();
            cboMaThuoc.DisplayMember = "TenThuoc";
            cboMaThuoc.ValueMember = "MaThuoc";
            //Load combox Nhà cung cấp
            cboMaNCC.DataSource = nhacungcapBUS.LayDSNhaCungCap();
            cboMaNCC.DisplayMember = "TenNCC";
            cboMaNCC.ValueMember = "MaNCC";
            // Load combox cho  mã thuốc trong DataGridView
            colMaThuoc.DataSource = dmThuocBUS.LayDanhMucThuoc();
            colMaThuoc.DisplayMember = "TenThuoc";
            colMaThuoc.ValueMember = "MaThuoc";
            // Load combox nhà cung cấp trong DataGridView
            colMaNCC.DataSource = nhacungcapBUS.LayDSNhaCungCap();
            colMaNCC.DisplayMember = "TenNCC";
            colMaNCC.ValueMember = "MaNCC";
            // load DataGridView sau cùng
            dataGridViewNhapThuoc.DataSource = thuocnhapBUS.LayDanhSachThuocNhap();
            EnableEditing(false);
        }

        private void dataGridViewNhapThuoc_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            // dòng hiện tại
            int dong = e.RowIndex;
            // thực thi binding data lên control, khi click vào row dataGridView
            txtMaHDNhap.Text = dataGridViewNhapThuoc.Rows[dong].Cells["MaHDNhap"].Value.ToString();
            cboMaThuoc.SelectedValue = dataGridViewNhapThuoc.Rows[dong].Cells["colMaThuoc"].Value.ToString();
            txtSoLuong.Text = dataGridViewNhapThuoc.Rows[dong].Cells["SoLuong"].Value.ToString();
            dtPickerNgayNhap.Text = dataGridViewNhapThuoc.Rows[dong].Cells["NgayNhap"].Value.ToString();
            cboMaNCC.SelectedValue = dataGridViewNhapThuoc.Rows[dong].Cells["colMaNCC"].Value.ToString();

        }
        private void EnableEditing(bool editing)
        {
            //button
            btnThem.Enabled = !editing;
            btnSua.Enabled = !editing;
            btnXoa.Enabled = !editing;
            btnLuu.Enabled = editing;
            btnKhongLuu.Enabled = editing;
            // texBox,Combobox
            txtMaHDNhap.Enabled = editing;
            cboMaThuoc.Enabled = editing;
            txtSoLuong.Enabled = editing;
            dtPickerNgayNhap.Enabled = editing;
            cboMaNCC.Enabled = editing;
            // DataGridView
            dataGridViewNhapThuoc.Enabled = !editing;
        }

        private void ResetTextValue()
        {
            txtSoLuong.Text = "";
        }
        private void btnThem_Click(object sender, EventArgs e)
        {
            EnableEditing(true);
            ResetTextValue();
            txtMaHDNhap.Text = thuocnhapBUS.NextID();
        }

        private void btnKhongLuu_Click(object sender, EventArgs e)
        {
            EnableEditing(false);
        }


        private NhapThuoc LayNhapThuoc()
        {
            NhapThuoc nhapthuoc = new NhapThuoc();
            nhapthuoc.MaHDNhap = txtMaHDNhap.Text;
            nhapthuoc.MaThuoc = cboMaThuoc.SelectedValue.ToString();
            // Chuyển đồi kiểu dữ liệu số
            nhapthuoc.SoLuong = Convert.ToDouble(txtSoLuong.Text);
            // chuyển đổi sang kiểu dử liệu DateTime
            nhapthuoc.NgayNhap = Convert.ToDateTime(dtPickerNgayNhap.Value.ToShortDateString());
            nhapthuoc.MaNCC = cboMaNCC.SelectedValue.ToString();
            return nhapthuoc;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {

            NhapThuoc nhapthuoc = LayNhapThuoc();
            // Nếu tồn tại mã bệnh nhân rồi thì là sửa
            if (thuocnhapBUS.CheckExists(nhapthuoc.MaHDNhap))
            {
                if (thuocnhapBUS.SuaThuocNhap(nhapthuoc))
                    frmNhapThuoc_Load(sender, e);

            }
            else // Thêm
            {
                if (thuocnhapBUS.ThemNhapThuoc(nhapthuoc))
                    frmNhapThuoc_Load(sender, e);
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            EnableEditing(true);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn Có Muốn Xóa Hóa Đơn: " + txtMaHDNhap.Text + "Không?", "Hỏi",
               MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                if (thuocnhapBUS.XoaNhapThuoc(txtMaHDNhap.Text))
                    // load lại form
                    frmNhapThuoc_Load(sender, e);
        }


    }
}
